using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LeftRight_Move : MonoBehaviour
{
    private Rigidbody2D rb;
    //private Animator anim;
    private Transform left, right, Boundaries;
    private float leftPoint, rightPoint;
    public float speed;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();

        Boundaries = transform.GetChild(0).GetChild(0);
        left = Boundaries.GetChild(0);
        right = Boundaries.GetChild(1);
        transform.GetChild(0).DetachChildren();

        leftPoint = left.position.x;
        rightPoint = right.position.x;
        Destroy(Boundaries.gameObject);
    }

    void FixedUpdate()
    {
        Movement();
    }

    void Movement()
    {
        rb.velocity = new Vector2(-speed * Time.deltaTime, rb.velocity.y);
        if (transform.position.x < leftPoint || transform.position.x > rightPoint)
        {
            speed = speed * -1;
            rb.velocity = new Vector2(-speed * Time.deltaTime, rb.velocity.y);
        }
    }
}
